package ru.papergames.model.proxies
{
	import mx.utils.ObjectUtil;
	import ru.papergames.model.proxies.HTTPProxy;
	import ru.papergames.model.vo.MessagingConnectVO;

	public class MsgServerAccessProxy extends MainHTTPProxy
	{
		
		public static const NAME:String = 'MsgServerAccessProxy';
		
		//methods
		public static const GET_DATA:String = 'getData';
		
		//responses
		public static const GET_DATA_START:String = GET_DATA + HTTPProxy.START_NOTE_POSTFIX;
		public static const GET_DATA_SUCCESS:String = GET_DATA + HTTPProxy.SUCCESS_NOTE_POSTFIX;
		public static const GET_DATA_FAULT:String = GET_DATA + HTTPProxy.FAULT_NOTE_POSTFIX;
		
		public function MsgServerAccessProxy(url:String)
		{
			super(NAME, url, new MessagingConnectVO());
			useOperationParam = false;
		}
		
		override public function onRegister():void {
			super.onRegister();			
			
		}
		
		override public function onRemove():void {
			super.onRemove();			
			
		}
		
		
		protected function get messagingProxy():MessagingSocketProxy {
			return this.getProxy(MessagingSocketProxy.NAME) as MessagingSocketProxy;
		}
		
		
		public function get connVO():MessagingConnectVO {
			return this.data as MessagingConnectVO;
		}
		
		override protected function parseResponse(data:XML, ownResponse:Boolean):void {
			
		}
		
		/*****  Call handlers  *****/
		
		/* getData */
		
		public function getData_Result(data:XML):String {
			var isSuccess:Boolean = String(data.data.success) == 'true';
			
			if (isSuccess) {
				var node:* = data.data.battle_connect_info;
				
				connVO.server = node.battle_server_host;
				connVO.port = node.battle_server_port;
				connVO.playerID = node.playerId;
				connVO.secretKey = node.secretKey;
				
				logger.debug( "Messaging Server Access: \n" + ObjectUtil.toString(connVO) );
				
				//try connect
				messagingProxy.tryConnect(connVO);
			}
			
			return isSuccess ? GET_DATA_SUCCESS : GET_DATA_FAULT;
		}
		
	}
}